import React from 'react';
import PageComponent from './PageComponent';
import { Table, Empty } from 'antd';
import { TableProps } from 'antd/lib/table';
import { PREFIX } from '../utils/consts';
import { TextWidget } from '@inbiz/react';
interface IDataTable {
  multiple?: boolean;
  className?: string;
  selectedRowKeys?: React.Key[];
  selectedRows?: Record<string, any>[];
  onSelectChange?: (keys: React.Key[], rows: Record<string, any>[]) => void;
  getCheckboxProps?: (row: Record<string, any>) => Record<string, any>;
  onChange: (pagination: { current: number; pageSize: number }) => void;
}

const DataTable: React.FC<Omit<TableProps<any>, 'onChange'> & IDataTable> = (
  props,
) => {
  const {
    multiple,
    className,
    selectedRowKeys = [],
    selectedRows = [],
    onSelectChange,
    getCheckboxProps,
    rowSelection,
    pagination,
    onChange,
    dataSource,
    ...rest
  } = props;

  return (
    <>
      <Table
        className={`${PREFIX}-dataTable ${className} ${
          dataSource && dataSource.length == 0 && `datatable-empty-data`
        }`}
        rowSelection={
          onSelectChange && {
            type: multiple ? 'checkbox' : 'radio',
            selectedRowKeys,
            onChange: onSelectChange,
            getCheckboxProps: getCheckboxProps,
            ...rowSelection,
          }
        }
        dataSource={dataSource}
        pagination={false}
        locale={{
          emptyText: (
            <Empty
              image={Empty.PRESENTED_IMAGE_SIMPLE}
              description={<TextWidget>MemberSelect.lang.noData</TextWidget>}
            />
          ),
        }}
        {...rest}
      />
      {pagination ? (
        <PageComponent
          options={[10, 20, 30, 40, 50]}
          className={`${PREFIX}-dataTable-pagination`}
          total={pagination.total || 0}
          current={pagination.current || 1}
          pageSize={pagination.pageSize || 0}
          onChange={(current, pageSize) => {
            onChange?.({ current, pageSize });
          }}
        />
      ) : null}
    </>
  );
};

export default DataTable;
